window.config="/service/package.json";
window.cs=new content_script();
function LoadPackage(callback){
    cs.sendBgInfo({
        type:"get_data",
        data:[config]
    },(info)=>{
        window.package=info.data;
        callback(package);
    })
}
function LoadScript(url,callback){
    cs.sendBgInfo({
        type:"get_text_data",
        data:[package.host+url]
    },(e)=>{
        eval(e.data);
        callback && callback(e);
    })
}
function LoadStyle(url,callback){
    cs.sendBgInfo({
        type:"get_text_data",
        data:[package.host+url]
    },(e)=>{
        cs.loadStyle(e.data);
        callback && callback(e);
    })
}
function StringOrArray(it,StrCall,ArrCall,ObjCall){
    if(typeof it === "string"){
        if(StrCall)StrCall();
    }else if(Array.isArray(it)){
        if(ArrCall)ArrCall();
    }else if(typeof it==="object"){
        if(ObjCall)ObjCall();
    }
}
function LoadHtml(it){
    StringOrArray(it,()=>{
        $("body").append(it);
    },null,()=>{
        if(it.type=="body_start"){
            $("body").before(it);
        }else{
            $("body").append(it);
        }
    })
}
function get_text_data(url,callback){
    cs.sendBgInfo({
        type:"get_text_data",
        data:[url]
    },(e)=>{
        callback && callback(e);
    })
}
function get_data(url,callback){
    cs.sendBgInfo({
        type:"get_data",
        data:[url]
    },(e)=>{
        callback && callback(e);
    })
}
function post_data(url,callback) {
    cs.sendBgInfo({
        type: "post_data",
        data: [url]
    }, (e) => {
        callback && callback(e);
    })
}
function ajax_data(obj,callback){
    cs.sendBgInfo({
        type:"ajax_data",
        data:[obj]
    },(e)=>{
        callback && callback(e);
    })
}
function open_url(url,callback){
    cs.sendBgInfo({
        type:"open_url",
        data:[url]
    },(e)=>{
        callback && callback(e);
    })
}
window.onload=()=>{
    LoadPackage((info)=>{
        let list=info.modules;
        let global=info.global;
        if(global){
            if(global.css){
                (function addCss(i){
                    if(i<global.css.length){
                        LoadStyle(global.css[i],()=>{
                            addCss(++i);
                        })
                    }
                })(0);
            }
            if(global.script){
                (function addScript(i){
                    if(i<global.script.length){
                        LoadScript(global.script[i],()=>{
                            addScript(++i);
                        })
                    }
                })(0);
            }
        }
        function LoadSource(it){
            if(it.script){
                StringOrArray(it.script,()=>{
                    LoadScript(it.script);
                },()=>{
                    it.script.forEach((script)=>{
                        LoadScript(script);
                    })
                });
            }
            if(it.css){
                StringOrArray(it.css,()=>{
                    LoadStyle(it.css);
                },()=>{
                    it.css.forEach((css)=>{
                        LoadStyle(css);
                    })
                });
            }
            if(it.html){
                StringOrArray(it.html,()=>{
                    LoadHtml(it.html);
                },()=>{
                    it.html.forEach((e)=>{
                        LoadHtml(e);
                    })
                },()=>{
                    LoadHtml(it.html);
                });
            }
        }
        if(list){
            let url=location.href;
            for (let i=0;i<list.length;i++){
                let it=list[i];
                if(it && it.path){
                    StringOrArray(it.path,()=>{
                        if(url.includes(it.path)){
                            LoadSource(it);
                        }
                    },()=>{
                        for(let j=0;j<it.path.length;j++){
                            if(url.includes(it.path[j])){
                                LoadSource(it);
                                break;
                            }
                        }
                    });
                }
            }
        }
    });
};
